package com.athuahua.spzx.controller.system;

import com.athuahua.spzx.annotation.AutoLog;
import com.athuahua.spzx.entity.SysRole;
import com.athuahua.spzx.dto.system.SysRoleDto;
import com.athuahua.spzx.entity.vo.SysRoleVo;
import com.athuahua.spzx.service.SysRoleService;
import com.athuahua.spzx.service.SysUserRoleService;
import com.athuahua.spzx.utils.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author: huahua
 * @Date: 2024/7/22 0022
 * @Time: 21:49
 * @Description:
 */
@Tag(name = "职位接口")
@RestController
@RequestMapping("/admin/system/sysRole")
@Slf4j
public class SysRoleController {
    @Resource
    private SysUserRoleService sysUserRoleService;

    @Resource
    private SysRoleService sysRoleService;

    @PostMapping("findByPage/{page}/{size}")
    @Operation( summary = "职位管理模块分页查询")
    public Result findByPage(@PathVariable("page") int page,
                             @PathVariable("size") int size,
                             @RequestBody SysRoleDto sysRoleDto) {
        log.info("职位管理分页查询 page: {} size: {} roleName: {}", page, size, sysRoleDto.getRoleName());


        //太抽象了！这么会有人前端要mybatis的pageInfo作为参数返回！！！！
        IPage<SysRole> list = sysRoleService.findByPage(page, size, sysRoleDto);

        SysRoleVo sysRoleVo = new SysRoleVo();
        sysRoleVo.setList(list.getRecords());
        sysRoleVo.setTotal((int) list.getTotal());

        return Result.success(sysRoleVo);
    }

    @PostMapping("saveSysRole")
    @Operation( summary = "职位管理模块新增角色")
    @AutoLog(title = "职位管理模块新增角色", businessType = 1) //记录日志使用
    public Result save(@RequestBody SysRole sysRole) {
        log.info("角色管理模块新增角色: {}", sysRole);

        return Result.success(sysRoleService.save(sysRole));
    }

    @DeleteMapping("deleteById/{roleId}")
    @Operation( summary = "职位模块删除角色")
    @AutoLog(title = "职位模块删除角色", businessType = 2) //记录日志使用
    public Result deleteById(@PathVariable("roleId") Long roleId) {
        log.info("删除职位: {}", roleId);
        sysRoleService.removeById(roleId);


        return Result.success(null);
    }

    @PutMapping("updateSysRole")
    @Operation( summary = "职位管理模块更新")
    @AutoLog(title = "职位管理模块更新", businessType = 3) //记录日志使用
    public Result update(@RequestBody SysRole sysRole) {
        log.info("更新职位: {}", sysRole);
        sysRoleService.updateById(sysRole);

        return Result.success(null);
    }


    //查询全部角色 findAllRoles
    @GetMapping("findAllRoles/{userId}")
    @Operation( summary = "查询全部职位")
    public Result findAllRoles(@PathVariable("userId") Long userId) {
        log.info("查询全部职位");
        //返回两部分角色，全部角色和用户已经操作过的角色
        Map<String, Object> map = sysRoleService.findAllRoles(userId);

        return Result.success(map);
    }

}
